package com.neiquan.weiguan.fragment.base;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.neiquan.weiguan.R;
import com.neiquan.weiguan.adapter.FragmentAdapter;
import com.neiquan.weiguan.colorful.Colorful;

import java.util.ArrayList;

/**
 * 作者:  hjb
 * 时间: 2016/3/3.
 */
public abstract class BaseViewPagerFragment extends BaseFragment {
    public RelativeLayout mHeadView;
    public TabLayout mTabLayout;
    public ViewPager mViewPager;
    public LinearLayout mMainContent;

    @Override
    public View initRootView() {
        rootView = View.inflate(getActivity(), R.layout.base_viewpage_fragment, null);
        mHeadView = (RelativeLayout) rootView.findViewById(R.id.head_view);
        mTabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
        mViewPager = (ViewPager) rootView.findViewById(R.id.base_fragment_viewPager);
        mMainContent = (LinearLayout) rootView.findViewById(R.id.main_content);
        return rootView;
    }

    private void setupViewPager() {
        String[] title = getTitle();
        ArrayList<Fragment> fragments = getFragments();
        if (title != null && title.length > 0 && fragments != null && fragments.size() > 0 && title.length == fragments.size()) {
            for (int i = 0; i < title.length; i++) {
                mTabLayout.addTab(mTabLayout.newTab().setText(title[i]));
            }
            FragmentAdapter adapter =
                    new FragmentAdapter(getChildFragmentManager(), fragments, title);
            mViewPager.setAdapter(adapter);
            mTabLayout.setupWithViewPager(mViewPager);
            mTabLayout.setTabsFromPagerAdapter(adapter);
        }
    }

    public abstract ArrayList<Fragment> getFragments();

    public abstract String[] getTitle();


    protected abstract boolean isHaveHead();


    @Override
    public void setViews() {
        setHeadView(isHaveHead());
        setupViewPager();
        setData(mViewPager.getCurrentItem());
        setupColorful();
    }

    /**
     * 返回第一个选中
     *
     * @param currentItem
     */
    protected void setSelectFragment(int currentItem) {
        mViewPager.setCurrentItem(currentItem);
    }

    /**
     * 当返回为true时候设置
     */
    protected abstract void setData(int currentItem);

    private void setHeadView(boolean isHaveHead) {
        if (!isHaveHead) {
            mHeadView.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    protected void setupColorful() {
        colorful = new Colorful.Builder(this)
                .backgroundColor(R.id.lin_base_viewpage_fragment_title, R.attr.title_bottom_view_bg)
                .backgroundColor(R.id.lin_base_fragment_menu, R.attr.menu_bg)
                .backgroundColor(R.id.base_fragment_viewPager, R.attr.root_view_bg)
                .create();
    }

    protected void setTitleTv(int id) {
        TextView tv = (TextView) rootView.findViewById(R.id.title_tv);
        tv.setText(id);
    }

    protected void setTitleTv(String str) {
        TextView tv = (TextView) rootView.findViewById(R.id.title_tv);
        tv.setText(str);
    }

    protected void setBackTv(String str) {
        TextView tv = (TextView) rootView.findViewById(R.id.back_tv);
        tv.setText(str);
    }

    protected void setNextTv(String str) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = rootView.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    protected TextView getNextTv() {

        return (TextView) rootView.findViewById(R.id.next_tv);
    }

    protected void setBackGone() {
        View tv = rootView.findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }

    protected void setBackOnClick(View.OnClickListener onClick) {
        LinearLayout ly_back = (LinearLayout) rootView.findViewById(R.id.ly_back);
        ly_back.setOnClickListener(onClick);
        View iv = rootView.findViewById(R.id.back_img);
        iv.setOnClickListener(onClick);
    }


    @SuppressLint("NewApi")
    protected void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    protected void setBackImage(int id) {
        ImageView iv = (ImageView) rootView.findViewById(R.id.back_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    protected void setNextImage(int id) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) rootView.findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    protected void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = rootView.findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }

    protected void setNextGone() {
        TextView tv = (TextView) rootView.findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) rootView.findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }
}
